require "ActionUtil"
--require "MainScene"
require "Hero"
require "FightData"
require "WarWinScene"

WarScene={}
WarSceneFunc={}
WarSceneFunc.__index = WarSceneFunc
setmetatable(WarScene,WarSceneFunc)
WarScene.NodeID = {EndFightButton=1001}
WarScene.leftPos =
                      {[1]={150, 400},[4]={250, 400},[7]={350, 400},
                      [2]={150, 300},[5]={250, 300},[8]={350, 300},
                      [3]={150, 200},[6]={250, 100},[9]={350, 100}}
WarScene.rightPos =
                      {[1]={960-350, 400},[4]={960-250, 400},[7]={960-150, 400},
                      [2]={960-350, 300},[5]={960-250, 300},[8]={960-150, 300},
                      [3]={960-350, 200},[6]={960-250, 200},[9]={960-150, 200}}
WarScene.leftFighters = nil 
WarScene.rightFighters = nil
WarScene.fightIndex = -1 
--[[FightView.rightPos =
                      {{75, 475},{175, 475},{275, 475}}
                      {{75, 325},{175, 325},{275, 325}}
                      {{75, 175},{175, 175},{275, 175}}
]]

--1|A|5|2|NN382|(),();1|D|2|5|NB64;1|A|5|2|BN181|(),();1|A|8|2|ND562|(),();1|D|3|5|NN107|(),();2|AS|5|0|P|(45,46,47);2|A|5|3|PN463|(45,46,47),();2|D|3|5|NN79|(),(45,46,47);2|A|8|3|ND605|(),()
local SoldierName = {[81]="qingtan", [42]="zhongtan", [49]="jifeng"}

WarSceneFunc.getNode = function(self, name, parent, data)
  local l = {}
  setmetatable(l,WarSceneFunc)
  l.node = CCBReader:load("WarScene.ccbi")
  print("WarScene.getNode", WarScene.node)
  --FightData.load(data)
  l.battle = Battle:create(data)
  l.name = name
  l.parent = parent
  l:init() 
  return l 
end

WarScene.getScene = function()
  local sc = CCScene:create()
  sc:addChild(WarScene:getNode().node)  
  print("WarScene.getScene", sc)
  return sc
end
--[[
WarScene.clear = function()
  for k, v in pairs(WarScene.leftFighters) do
    v:release()
  end
  for k, v in pairs(WarScene.rightFighters) do
    v:release()
  end
  WarScene.leftFighters = nil
  WarScene.rightFighters = nil
end]]

WarSceneFunc.init = function(self)
  self.isEnd = false
  local b = self.node:getChildByTag(WarScene.NodeID.EndFightButton)
  setObjectExtTable(b, self)

  self.leftFighters = {} 
  self.rightFighters = {} 
  --FightData.load()
  self.fightSeq = self.battle.fightSeq
  print("WarScene.init WarScene.fightSeq", self.fightSeq, self.fightSeq:size())
  self.fightIndex = -1
  for k,v in pairs(self.battle.leftArmy) do
    local pos = WarScene.leftPos[v.location]
    local name = SoldierName[v.soldier_id]
    if name==nil then name="qingtan"; end
    --print(v.soldier_id, v.location,  name, pos[1], pos[2])
    local h = Hero:create(self.node, v.g, v.soldier_id, ccp(pos[1],pos[2]), "onHeroStateChanged", self)
    h.pos = v.location
    h.onHeroStateChanged = onHeroStateChanged
    self.leftFighters[v.location] = h
    --print("WarScene.init left", name, v.location)
  end
  for k,v in pairs(self.battle.rightArmy) do
    local pos = WarScene.rightPos[v.location]
    local name = SoldierName[v.soldier_id]
    if name==nil then name="qingtan"; end
    --print(v.soldier_id, v.location, name, pos[1], pos[2])
    local h = Hero:create(self.node, v.g, v.soldier_id, ccp(pos[1],pos[2]), "onHeroStateChanged", self)
    h.normalView:setFlipX(true)
    h.onHeroStateChanged = onHeroStateChanged
    h.pos = v.location
    self.rightFighters[v.location] = h
    --print("WarScene.init right", name, v.location)
  end
  self:playNext()
end

WarSceneFunc.getAttacker = function(self, fight)
  if fight.direction=="A" then
    return self.leftFighters[fight.attackPos]
  else
    return self.rightFighters[fight.attackPos]
  end
end

WarSceneFunc.getDefenders = function(self, fight)
  local arr = {} 
  for k, v in pairs(fight.defend) do
    if fight.direction=="A" then
      arr[k] = self.rightFighters[k]
      self.rightFighters[k].hurt = v.hurt
    else
      arr[k] = self.leftFighters[k]
      self.leftFighters[k].hurt = v.hurt
    end
  end
  return arr
--[[  
  local arr = Array:create()
  for k, v in pairs(fight.defend) do
    if fight.direction=="A" then
      arr:append(self.rightFighters[k])
      self.rightFighters[k].hurt = v.hurt
    else
      arr:append(self.leftFighters[k])
      self.leftFighters[k].hurt = v.hurt
    end
  end
  return arr
]]
end

WarSceneFunc.endFight = function(self)
  print("WarScene.endFight")
  if self.isEnd==false then
    --local metaFunc = getmetatable(self.zhandouScene)
    --metaFunc["showWinWnd"](self.zhandouScene, FightData.json.data.gainXp, FightData.json.data.gainCash, FightData.json.data.evaluate)
    --[[print("ZhanDouSceneFunc.showWinWnd", FightData.json.data.gainXp, FightData.json.data.gainCash, FightData.json.data.evaluate)
    local exp = 0--FightData.json.data.gainXp 
    local cash = 0--FightData.json.data.gainCash 
    local eval = 1--FightData.json.data.evaluate
    if FightData.json.data.gainXp~=nil then exp=FightData.json.data.gainXp end
    if FightData.json.data.gainCash~=nil then cash=FightData.json.data.gainCash end
    if FightData.json.data.evaluate~=nil then eval=FightData.json.data.evaluate end
    ]]
    --local cid = :getCheckpointInfoID(self.currentStronghold, self.currentCheckpoint)
    --UserData.userinfo.host.evaluate[cid+1] = eval
    --print(self.currentStronghold, self.currentCheckpoint, cid, eval)
    --local info = self:getCheckpointInfo(self.currentStronghold, self.currentCheckpoint)
    if self.battle:isVictory()==true then 
    --if exp~=nil and cash~=nil and eval~=nil then
      --WarResultScene:showWinWnd(self.parent, self.name, exp, cash, eval)
      WarResultScene:showWinWndExt(self.parent, self.name, self.battle) 
    else
      WarResultScene:showLoseWnd(self.parent)
    end
    self.node:getParent():removeChild(self.node, true)
  end
  self.isEnd = true
  --WarScene.clear()
  --CCDirector:sharedDirector():replaceScene(CCTransitionSlideInR:create(1,WarWinScene.getScene()))
end

WarSceneFunc.playNext = function(self)
  self.fightIndex = self.fightIndex + 1
  local aFight = self.fightSeq:at(self.fightIndex)
  print("WarScene.playNext", self.fightIndex)
  print("WarScene.getAttacker", aFight.direction, aFight.attackPos)
  local h = self:getAttacker(aFight)
  self.curDefenders = self:getDefenders(aFight) 
  --if tableSize(self.curDefenders)>0 then
  if false then 
    h:setHeroState(Hero.HeroState.Fire) 
    --WarScene.waitCount = aFight.defend:size()
  else
    --h.skillName="tiebi"
    print("WarScene.playNext", Hero.HeroState.Skill)
    h:setHeroState(Hero.HeroState.Skill)
  end
end

WarSceneFunc.onHeroStateChanged = function(self, hero)
  local aFight = self.fightSeq:at(self.fightIndex)
  local attacker = self:getAttacker(aFight)
  --local defenders = self:getDefenders(aFight) 
  --print("onHeroStateChanged", hero, hero.pos, attacker, defenders:size(), aFight)
  
  if hero.currentState==Hero.HeroState.Fire or hero.currentState==Hero.HeroState.Skill then
    hero:setHeroState(Hero.HeroState.Normal)
    if tableSize(self.curDefenders)==0 then
      self:playNext()
    else
      for k,v in pairs(self.curDefenders) do 
      --for i=0,defenders:size()-1 do
        --print("onHeroStateChanged", defenders:at(i), defenders:at(i).pos)
        v:setHeroState(Hero.HeroState.Hurt)
      end 
    end
  elseif hero.currentState==Hero.HeroState.Hurt then
    hero:setHeroState(Hero.HeroState.Hurted) 
  elseif hero.currentState==Hero.HeroState.Hurted then
    hero:setHeroState(Hero.HeroState.Normal)
    print("--", aFight.defend)
    print("--", hero.pos, aFight.defend[hero.pos])
    local state = aFight.defend[hero.pos].defendType
    local buf = aFight.defend[hero.pos].buf
    --print("onAction defend end", WarScene.fightIndex, hero.pos, state, buf)
    if state=="D" then
      hero:release()
    else
      if buf=="" then end
    end
    self.curDefenders[hero.pos] = nil
 
    --多目标
    if tableSize(self.curDefenders)==0 then
      if self.fightIndex < self.fightSeq:size()-1 then
        self:playNext()
      else
        self:endFight()
      end
    end
    --[[if tostring(hero) == tostring(defenders:at(0)) then
      --print("WarScene.onAction begin next fight", WarScene.fightIndex)
      if self.fightIndex < self.fightSeq:size()-1 then
        self:playNext()
      else
        self:endFight()
      end
    end]]
  end
end

WarSceneFunc.onEndFightButton = function(self, node, event)
  --self:init()
  print("WarSceneFunc.onEndFightButton")
  --self.node:stopAllActions()
  self:endFight()  
end
